<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//社区用户表
class Bbs_user_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->table = 'users';
        $this->users_info = 'users_info';
        $this->users_spread = 'users_spread';
        $this->bbs_account = 'bbs_account';
        $this->bbs_user = 'bbs_user';
        $this->bbs_count = 'bbs_count';
        $this->bbs_user_group = 'bbs_user_group';
        $this->bbs_fans = 'bbs_fans';
        $this->bbs_user_log = 'bbs_user_log';
        $this->bbs_visit_list = 'bbs_visit_list';
    }
    //获取用户社区信息
    function get_users($user_id){
        $this->db->select($this->bbs_user.'.*,'.$this->users_info.'.*,'.$this->bbs_account.'.*,'.$this->table.".*");
        $this->db->from($this->bbs_user);
        $this->db->join($this->users_info, $this->users_info.'.user_id = '.$this->bbs_user.'.user_id', 'left');
        $this->db->join($this->bbs_account, $this->bbs_account.'.user_id = '.$this->bbs_user.'.user_id', 'left');
        $this->db->join($this->table, $this->bbs_user.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->where($this->bbs_user.'.user_id',$user_id);
        return $this->db->get()->row_array();
    }
    
    function get($uid){
        $this->db->select($this->table.'.*,'.$this->users_info.'.*,'.$this->bbs_account.'.*');
        $this->db->from($this->table);
        $this->db->join($this->users_info, $this->users_info.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->join($this->bbs_account, $this->bbs_account.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->where($this->table.'.user_id',$uid);
        return $this->db->get()->row_array();
    }
    //获取指定某个字段的信息
    function get_user_info_by_column($user_id,$column){
        $query = $this->db->select($column)->where('user_id',$user_id)->get($this->users_info)->row_array();
        return $query[$column];
    }
    
    //创建社区用户表
    function insert_bbs_user($data){
        $this->db->insert($this->bbs_user, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    //创建社区账户表
    function insert_bbs_account($data){
        $this->db->insert($this->bbs_account, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    //创建社区用户个人统计表
    function insert_bbs_count($data){
        $this->db->insert($this->bbs_count, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    //获取社区用户统计信息
    function get_bbs_count($uid){
        return $this->db->where('user_id',$uid)->get($this->bbs_count)->row_array();
    }
    
    //更新社区用户统计信息
    function update_bbs_count($data,$uid){
        return $this->db->where('user_id',$uid)->update($this->bbs_count, $data);
    }
    
    function bbs_user($user_id){
        $this->db->select("*")->from($this->bbs_account)->where('user_id',$user_id);
        return $this->db->get()->num_rows();
    }
    function reg_bbs($data){
        $this->db->insert($this->bbs_account,$data);
        $this->db->insert($this->bbs_user,$data);
        $this->db->insert($this->bbs_count,$data);
    }
    function get_user_by_uid($uid=0){
        return $this->db->where('user_id',$uid)->get($this->table)->row_array();
    }
    
    function get_user_by_username($username){
        return $this->db->where('username',$username)->get($this->table)->row_array();
    }
    
    function get_user_by_mobile($mobile){
        return $this->db->where('mobile',$mobile)->get($this->table)->row_array();
    }
    
    function get_user_by_email($email){
        return $this->db->where('email',$email)->get($this->table)->row_array();
    }
    
    
    function get_user_by_condition($where){
        
        return $this->db->select($this->bbs_user.".*,".$this->users_info.".nickname,".$this->users_info.".head_img,".$this->users_info.".signature")->from($this->bbs_user)->join($this->users_info,$this->bbs_user.".user_id = ".$this->users_info.".user_id","left")->where($where)->get()->row_array();
    }
    
    function insert($data){
        $this->db->insert($this->table, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    function update($data){
        return $this->db->where('user_id',$this->session->userdata('id'))->update($this->table, $data);
    }
    
    function user_update($data,$uid){
        return $this->db->where('user_id',$uid)->update($this->table, $data);
    }
    
    function user_info_update($data,$uid){
        return $this->db->where('user_id',$uid)->update($this->users_info, $data);
    }
    
    function insert_user_info($data)
    {
        $this->db->insert($this->users_info, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    function get_user_group($where){
        return $this->db->where($where)->get($this->bbs_user_group)->row_array();
    }
    
    //判断是否关注
    function get_fan_status($where){
        $this->db->select("*");
        $this->db->from($this->bbs_fans);
        $this->db->where($where);
        return $this->db->get()->num_rows();
    }
    
    //增加粉丝记录
    function insert_user_fan($data){
        $this->db->insert($this->bbs_fans,$data);
        if($this->db->affected_rows()==1){      //评论成功记录评论数
           $this->db->set('fans_count','fans_count + 1',FALSE);
           $this->db->where('user_id',$data['user_id']);
           $this->db->update($this->bbs_count);
           
           $this->db->set('following_count','following_count+1',FALSE);
           $this->db->where('user_id',$data['followuid']);
           $this->db->update($this->bbs_count); 
           
        }
        return $this->db->affected_rows();
    }
    
    //删除粉丝数
    function delete_user_fan($where){
        $this->db->where("user_id",$where['user_id'])->where("followuid",$where['followuid'])->delete($this->bbs_fans);
        if($this->db->affected_rows()==1){
           $this->db->set('fans_count','fans_count - 1',FALSE);
           $this->db->where('user_id',$where['user_id']);
           $this->db->update($this->bbs_count);
           
           $this->db->set('following_count','following_count-1',FALSE);
           $this->db->where('user_id',$where['followuid']);
           $this->db->update($this->bbs_count); 
        }
        return $this->db->affected_rows();
    }
    //用户日志
    function user_log($data){
        $this->db->insert($this->bbs_user_log, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }        
    }
    //获取访问状态
    function get_visit_status($where){
        $this->db->select("*");
        $this->db->from($this->bbs_visit_list);
        $this->db->where($where);
        return $this->db->get()->num_rows();
    }
    //访问记录
    function visit($data){
        $this->db->insert($this->bbs_visit_list, $data);
    }
    //重复访问记录
    function rep_visit($where){
        $this->db->set('num','num+1',FALSE);
        $this->db->where($where);
        $this->db->update($this->bbs_visit_list); 
    }        
    
}
